package com.zch.spring_demo.base.service;

import com.zch.spring_demo.base.domain.BaseDomain;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import java.io.Serializable;
import java.util.List;

/**
 * @author biscuit
 * @data 2018/01/16
 * Spring jpa在service层的衍生,使项目开发的时候,更容易使用jpa API,不需要再写引用jpa API相关方法的实现
 */
public abstract class BaseServiceImpl<ID extends Serializable,CLZ extends JpaRepository<T, ID>,T extends BaseDomain> implements IBaseService<T,ID>{
    private  JpaRepository dao ;

    public BaseServiceImpl(CLZ clz){
        this.dao = clz;
    }
    /**增OR改*/
    @Override
    public void save(T entity) {
        dao.save(entity);
    }
    /**删*/
    @Override
    public void delete(T entity) {
        dao.delete(entity);
    }
    /**查所有*/
    @Override
    public List<T> findAll() {
        return (List<T>) dao.findAll();
    }
    @Override
    public List<T> findAll(Sort var){
       return  dao.findAll(var);
    }
    @Override
    public List<T> findAll(Iterable<ID> id){
        return dao.findAll(id);
    }
    @Override
    public <S extends T> List<S> save(Iterable<S> var){
        return dao.save(var);
    }
    @Override
    public void flush(){
        dao.flush();
    }
    @Override
    public  <S extends T> S saveAndFlush(S var){
        return (S) dao.saveAndFlush(var);
    }
    @Override
    public void deleteInBatch(Iterable<T> var){
        dao.deleteInBatch(var);
    }
    @Override
    public void deleteAllInBatch(){
        dao.deleteAllInBatch();
    }
    @Override
    public T getOne(ID id){
        return (T) dao.getOne(id);
    }
    @Override
   public  <S extends T> List<S> findAll(Example<S> var1){
        return dao.findAll(var1);
    }
    @Override
    public  <S extends T> List<S> findAll(Example<S> var1, Sort var2){
       return dao.findAll(var1,var2);
    }
}
